trigger AddRecordToOutletMonthlyOfftake on Inventory__c (after insert) {
    
    //以下是对OutletMonthlyOfftake__c对象的逻辑处理
    //DateTime dtime=trigger.new[0].VisitDate__c;
    //Date dt=Date.newInstance(dtime.year(),dtime.month(), 1);
    //查找当前月的数据
    //List<OutletMonthlyOfftake__c> omoList=[select OutletName__c,ProductName__c,MonthlyOfftakeDate__c 
                                            //from OutletMonthlyOfftake__c where MonthlyOfftakeDate__c=:dt];
   String strF='';
    
    for(Inventory__c iventory:trigger.new)
    {
        String dateStr=String.ValueOf(iventory.VisitDate__c.year());               
        if(iventory.VisitDate__c.month()<10)                    
            dateStr+='-0'+String.ValueOf(iventory.VisitDate__c.month());               
        else                    
            dateStr+='-'+String.ValueOf(iventory.VisitDate__c.month());               
        //if(iventory.VisitDate__c.day()<10)                    
            dateStr+='-01';               
        //else                    
        //  dateStr+='-'+String.valueOf(iventory.VisitDate__c.day());
                
        if(strF!='')
        {
            strF+=' or ';
        }
        strF+='MonthlyOfftakeDate__c='+dateStr+'';
    }
    String sqlF='select OutletName__c,ProductName__c,MonthlyOfftakeDate__c from OutletMonthlyOfftake__c where OwnerId=\''+UserInfo.getUserId()+'\' and('+strF+')';
   List<OutletMonthlyOfftake__c> omoList=DataBase.query(sqlF);
  
  List<OutletMonthlyOfftake__c> allInsertOfftake=new List<OutletMonthlyOfftake__c>();
  List<OutletMonthlyOfftake__c> allUpdateOfftake=new List<OutletMonthlyOfftake__c>();
  
  Map<Id,OutletMonthlyOfftake__c> mapList=new Map<Id,OutletMonthlyOfftake__c>(); 
  for(OutletMonthlyOfftake__c omo:omoList) 
    mapList.put(omo.OutletName__c,omo);
                                            
  for(Inventory__c ivt:trigger.new)
  {
    if(mapList.containsKey(ivt.OutletId__c))//比较门店
    {
        OutletMonthlyOfftake__c omoff=mapList.get(ivt.OutletId__c);
        if(omoff.ProductName__c==ivt.Product_Name__c)//比较产品
        {
            OutletMonthlyOfftake__c newOmoff=omoff;
            newOmoff.InventoryVolume__c=ivt.Inventory__c;
            newOmoff.InventoryValue__c=ivt.Inventory__c*ivt.DistributorPricePreTax__c;
            allUpdateOfftake.add(newOmoff);
            //ivt.addError('ddddddd');
        }
        else
        {
            
            OutletMonthlyOfftake__c om=new OutletMonthlyOfftake__c();
            om.OutletName__c=ivt.OutletId__c;
            om.ProductName__c=ivt.Product_Name__c;
            //om.MonthlyOfftakeDate__c=dt;
            om.InventoryVolume__c=ivt.Inventory__c;
            if(ivt.Inventory__c==NULL)
                om.InventoryValue__c=0;
            else
                om.InventoryValue__c=ivt.Inventory__c*ivt.DistributorPricePreTax__c;
            
            DateTime d=ivt.VisitDate__c;
            om.MonthlyOfftakeDate__c=Date.newInstance(d.year(),d.month(),1);
            om.OwnerId=UserInfo.getUserId();
            allInsertOfftake.add(om);
        }
    }
    else
    {
        OutletMonthlyOfftake__c om=new OutletMonthlyOfftake__c();
        om.OutletName__c=ivt.OutletId__c;
        om.ProductName__c=ivt.Product_Name__c;
        //om.MonthlyOfftakeDate__c=dt;
        om.InventoryVolume__c=ivt.Inventory__c;
        if(ivt.Inventory__c==NULL)
            om.InventoryValue__c=0;
        else
            om.InventoryValue__c=ivt.Inventory__c*ivt.DistributorPricePreTax__c;
            
        om.OwnerId=UserInfo.getUserId();
        DateTime d=ivt.VisitDate__c;
        om.MonthlyOfftakeDate__c=Date.newInstance(d.year(),d.month(),1);        
        allInsertOfftake.add(om);
    }
  }
  if(allUpdateOfftake.size()>0)
    update allUpdateOfftake;
  if(allInsertOfftake.size()>0)
    insert allInsertOfftake;
    
    
 //以下对TradeDashboardDetails__c对象的逻辑处理---------------------------------------------------------------/
     
    List<TradeDashboardDetails__c> ssyList=new List<TradeDashboardDetails__c>();
    ScoreStandard__c score=ScoreStandard__c.getValues('Distribution Point');
    List<String> MonthList=new List<String>();
    List<String> YearList=new List<String>();
    List<Date> DateList=new List<Date>();
    List<Id> lastActual=new List<Id>();
    List<Date> lastDate=new List<Date>();
    
    String strM='',strY='';
    for(Inventory__c st:trigger.new)
    {
        DateTime dt=st.VisitDate__c;
        Date d=Date.newInstance(dt.year(),dt.month(),1);
        MonthList.add(String.valueof(dt.month()));
        YearList.add(String.valueOf(dt.year()));
        DateList.add(d);
        lastActual.add(st.OutletId__c);
        
        lastDate.add(d.addMonths(-1));
    }
    
    List<Sales_Target__c> targetList=[select TotalValue__c,Month__c,Year__c,NextSales__c,Sales_Target_Type__c from Sales_Target__c 
                                        where NextSales__c=:UserInfo.getUserId() and Month__c in: MonthList
                                        and Year__c in:YearList and Sales_Target_Type__c='Distributed Outlet Number'];
    
   
    List<TradeDashboardDetails__c> inSvList=[select ownerid,Outlet__c,SalesMonth__c
     from TradeDashboardDetails__c where OwnerId=:UserInfo.getUserId() and SalesMonth__c in:DateList and CriticalEnabler__c='Distribution Point'];
     
     
     List<TradeDashboardDetails__c> getLastMonthData=[select Outlet__c,Actual__c from TradeDashboardDetails__c where OwnerId=:UserInfo.getUserId() and SalesMonth__c in:lastDate and CriticalEnabler__c='Distribution Point' and Outlet__c in:lastActual];
     Map<Id,String> lastActualMap=new Map<Id,String>();
     //取出上个月的执行次数加入到map中
     if(getLastMonthData !=NULL && getLastMonthData.size()>0)
     {
        for(TradeDashboardDetails__c t:getLastMonthData)
        {
            if(lastActualMap.containsKey(t.Outlet__c))
            {
                Integer i=Integer.valueof(lastActualMap.get(t.Outlet__c));
                lastActualMap.put(t.Outlet__c,String.valueOf(i+Integer.valueOf(t.Actual__c)));
            }
            else
            {
                //trigger.new[0].addError(String.valueof(t.Actual__c));
                if(t.Actual__c!=NULL)
                    lastActualMap.put(t.Outlet__c,String.valueOf(t.Actual__c));
                else
                    lastActualMap.put(t.Outlet__c,'0');
            }
        }
     }
     else
     {
        for(Inventory__c st:trigger.new)
        {
            lastActualMap.put(st.OutletId__c,'0');
        }
     }
     
     Map<Id,Date> mapList1=new Map<Id,Date>();
     Map<Date,String> mapList2=new Map<Date,String>();
     
     if(targetList.size()>0)//写入SalesTarget到map中
     {
         for(Sales_Target__c i:targetList)
         {
            Date d=Date.newInstance(Integer.valueOf(i.year__c),Integer.valueOf(i.Month__c),1);
            //trigger.new[0].addError(i.Month__c+'dddd'+String.valueof(i.TotalValue__c));
            mapList2.put(d,String.valueof(i.TotalValue__c));
         }
     }
     else
     {
        for(Inventory__c st:trigger.new)
        {
            DateTime dt=st.VisitDate__c;
            Date d=Date.newInstance(dt.year(),dt.month(),1);
            mapList2.put(d,String.valueof(0));
        }
     }
         
     if(inSvList!=NULL && inSvList.size()>0)
     {
         for(TradeDashboardDetails__c ssy:inSvList)
         {
            mapList1.put(ssy.Outlet__c,ssy.SalesMonth__c);
         }
         
         for(Inventory__c isvisit:trigger.new)
        {
            if(!mapList1.containsKey(isvisit.OutletId__c))
            {
                if(isvisit.Display__c>0 || isvisit.Inventory__c>0 || isvisit.Retail_Price__c>0)
                {
                    DateTime dt=isvisit.VisitDate__c;
                    Date d=Date.newInstance(dt.year(),dt.month(),1);
                    Integer TotalNum=Integer.valueof(mapList2.get(d));
                    
                    Integer lastActualTotal=Integer.valueOf(lastActualMap.get(isvisit.OutletId__c));
                    
                    TradeDashboardDetails__c sae=new TradeDashboardDetails__c();
                    sae.CriticalEnabler__c='Distribution Point';
                    sae.Outlet__c=isvisit.OutletId__c;
                    sae.ScoreStandard__c=score.Standard__c;
                    sae.LastMonthActual__c=lastActualTotal;
                    sae.Actual__c=1;
                    
                    if(inSvList!=NULL && inSvList.size()>0)//如果同一个TSR在TradeDashboardDetails__c里有值说明已经存入了SalesTarget
                        sae.Target__c=0;
                    else
                        sae.Target__c=TotalNum;
                    
                    sae.OwnerId=UserInfo.getUserId();
                    sae.SalesMonth__c=d;
                    ssyList.add(sae);
                }
            }
        }
     }
     else
     {
        for(Inventory__c isvisit:trigger.new)
        {
            if(isvisit.Display__c>0 || isvisit.Inventory__c>0 || isvisit.Retail_Price__c>0)
            {
                DateTime dt=isvisit.VisitDate__c;
                Date d=Date.newInstance(dt.year(),dt.month(),1);
                Integer TotalNum=Integer.valueof(mapList2.get(d));
                //trigger.new[0].addError(String.valueOf(lastActualMap.size()));
                Integer lastActualTotal=Integer.valueOf(lastActualMap.get(isvisit.OutletId__c));
                
                TradeDashboardDetails__c sae=new TradeDashboardDetails__c();
                sae.CriticalEnabler__c='Distribution Point';
                sae.Outlet__c=isvisit.OutletId__c;
                sae.ScoreStandard__c=score.Standard__c;
                sae.LastMonthActual__c=lastActualTotal;
                sae.Actual__c=1;
                if(inSvList!=NULL && inSvList.size()>0)//如果同一个TSR在TradeDashboardDetails__c里有值说明已经存入了SalesTarget
                    sae.Target__c=0;
                else
                    sae.Target__c=TotalNum;
                
                sae.OwnerId=UserInfo.getUserId();
                sae.SalesMonth__c=d;
                ssyList.add(sae);
            }
            
        }
     }
     
     insert ssyList; 
    
    /*Map<Id,Id> middleMap=new Map<Id,Id>();
    for(MiddleProcessDetails__c ml:middleList)//放入map中去
    {
        middleMap.put(ml.OutletName__c,ml.OutletName__c);
    }
    List<MiddleProcessDetails__c> mpdList=new List<MiddleProcessDetails__c>();
    for(Inventory__c iventory:trigger.new)
    {
        if(!middleMap.containsKey(iventory.OutletId__c))
        {
            if(iventory.Display__c>0 && iventory.Inventory__c>0 && iventory.Retail_Price__c>0)
            {
                MiddleProcessDetails__c mp=new MiddleProcessDetails__c();
                mp.Actual__c=1;
                mp.CriticalEnabler__c='Distribution Point';
                mp.MonthDate__c=dt;
                mp.OutletName__c=iventory.OutletId__c;
                mp.ownerid=UserInfo.getUserId();
                mpdList.add(mp);
            }
        }
    }
    insert mpdList;*/
}